﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace DLL.FriendlyLink
{
	/// <summary>
	/// 视图控制类
	/// </summary>
	/// <![CDATA[2014-8-7 17:52:59]]>
	public class View : DLL.ShowLayer.FormView
	{
		private Component clsthisComp;

		#region 添加、修改
		private TextBox tbName = new TextBox();
		private TextBox tbLink = new TextBox();
		private FileUpload fUpload = new FileUpload();
		private Image imgPic = new Image();
		private TextBox tbOrder = new TextBox();

		#endregion 添加、修改

		#region 查询

		#endregion 查询

		#region 浏览

		#endregion 浏览

		#region 构造函数
		/// <summary>
		/// 视图类构造函数
		/// </summary>
		/// <![CDATA[2014-9-9 17:25:12]]>
		public View(System.Web.UI.Page page)
			: base(page)
		{
			table.ID = "201409092317";

			clsthisComp = new Component(page);

			clsComp = clsthisComp;

			imgPic.CssClass = "Pic";

			listAddEdit.Add(new TonyNet4V1.Structs.TitleControlEntity("名称", "TextBox", tbName, "string", "strName"));
			listAddEdit.Add(new TonyNet4V1.Structs.TitleControlEntity("地址", "TextBox", tbLink, "string", "strLink"));
			listAddEdit.Add(new TonyNet4V1.Structs.TitleControlEntity("顺序", "TextBox", tbOrder, "int", "nOrder"));
			listAddEdit.Add(new TonyNet4V1.Structs.TitleControlEntity("图片", "FileUpload", fUpload, "", ""));
			listAddEdit.Add(new TonyNet4V1.Structs.TitleControlEntity("", "Image", imgPic, "", ""));
		}
		#endregion 构造函数

		#region 类型转换

		protected override TonyNet4V1.EntityBase GetEntity()
		{
			return new Entity();
		}

		protected override TonyNet4V1.QueryEntityBase GetQuery()
		{
			return new QueryEntity();
		}

		protected override TonyNet4V1.ComponentBase GetComponent()
		{
			return clsthisComp;
		}

		#endregion 类型转换

		public override void ShowAdd()
		{
			base.ShowAdd();
			Table tableAddEdit = new Table();

			tableAddEdit = (Table)page.FindControl(table.ID);

			tableAddEdit.Rows[tableAddEdit.Rows.Count - 1].Visible = false;

		}

		public override void ShowEdit()
		{
			base.ShowEdit();

			Table tableAddEdit = new Table();

			tableAddEdit = (Table)page.FindControl(table.ID);

			tableAddEdit.Rows[tableAddEdit.Rows.Count - 1].Visible = true;

		}

		/// <summary>
		/// 添加
		/// </summary>
		/// <returns></returns>
		public override bool Add()
		{
			//return base.Add();
			FileContent.Component clsFileContentComp = new FileContent.Component(page);
			FileContent.Entity clsFileContent = new FileContent.Entity();
			FriendlyLink.Entity clsFriendlyLink = new Entity();

			if (fUpload.HasFile)
			{
				clsFileContent = clsFileContentComp.Add(fUpload);
			}

			ControlToEntity(clsFriendlyLink, listAddEdit);

			clsFriendlyLink.ImageID = clsFileContent.ID;

			return clsthisComp.Add(clsFriendlyLink);
			
		}

		/// <summary>
		/// 修改
		/// </summary>
		/// <param name="nUpdateID"></param>
		/// <returns></returns>
		public override bool Update(int nUpdateID)
		{
			//return base.Update(nUpdateID);
			Entity clsFriendlyLink = new Entity();
			FileContent.Component clsFileContentComp = new FileContent.Component(page);
			FileContent.Entity clsFileContent = new FileContent.Entity();

			clsFriendlyLink = clsthisComp.GetOne(nUpdateID);
			ControlToEntity(clsFriendlyLink, listAddEdit);
			if (fUpload.HasFile)
			{
				clsFileContent = clsFileContentComp.GetOne(clsFriendlyLink.ImageID);
				clsFileContentComp.Modify(fUpload, clsFileContent);
			}

			return clsthisComp.Modify(clsFriendlyLink);
		}

		public override bool Edit(int nEditID)
		{
			Entity clsFriendlyLink = new Entity();

			clsFriendlyLink = clsthisComp.GetOne(nEditID);

			imgPic.ImageUrl = "~/ShowFileContent.aspx?FileContentID=" + clsFriendlyLink.ImageID.ToString();

			return base.Edit(nEditID);
		}
	}
}
